<template>
	<Uselect class="input-r" v-model="value" :list="list" :keyName="setting[types].keyName"
		:keyId="setting[types].keyId" @inChange="inChange" :placeholder="setting[types].placeholder" :name="name" />
</template>

<script>
	import Uselect from '@/pages/releases/components/select.vue';
	import {
		getBrandListApi
	} from '@/api/brand/index.js'
	import {
		ProjectList,
		StoreList
	} from '@/api/subletOfShop.js';
	export default {
		props: {
			dataform: {
				type: Object,
				default: () => ({})
			},
			types: {
				type: [Number, String], //0 项目  1品牌  2 经营户
				default: 0
			},
		},
		components: {
			Uselect
		},
		watch: {
			dataform: {
				handler() {

					this.toTb();

				},
				deep: true,
				immediate: true
			},
			value() {
				this.toTb();
			},
			name() {

			}
		},
		mounted() {
			this.getList();
		},
		data() {
			return {
				name: '',
				value: '',
				list: [],
				inited: false,
				setting: {
					0: {
						placeholder: '请选择项目',
						keyId: 'id',
						keyName: 'name',
						valueKey: 'projectId',
						valueName: 'projectName',
						filter: 'name',
						params: {
							city: this.$city
						}
					},
					1: {
						placeholder: '请选择品牌',
						keyId: 'id',
						keyName: 'realname',
						valueKey: 'brandId',
						valueName: 'brandName',
						filter: 'realname'
					},
					2: {
						placeholder: '请选择',
						keyId: 'id',
						keyName: 'storename',
						valueKey: 'store',
						valueName: 'storeName',
						filter: 'storename',
						params: {
							province: this.$cityCode[0],
							city: this.$cityCode[1]
						}
					},
				},
				meth: {
					0: ProjectList,
					1: getBrandListApi,
					2: StoreList
				}
			}
		},
		methods: {
			toTb() {
				if (!this.inited && this.dataform.id) {
					this.name = this.dataform[this.setting[this.types].valueName];
					this.inited = true;
				} else if (this.dataform[this.setting[this.types].valueKey] != this.value) {
					this.name = this.dataform[this.setting[this.types].valueName];
					this.$set(this.dataform, this.setting[this.types].valueKey, this.value)
					this.$emit('change', this.setting[this.types].valueKey, this.value)
					// this.dataform,[this.setting[this.types].valueKey] = this.value;
				}
			},
			getList(val) {
				let params = {};
				if (val) {
					params[this.setting[this.types].filter] = val
				}
				if (this.setting[this.types].params) {
					params = {
						...params,
						...this.setting[this.types].params
					}
				}
				this.meth[this.types](params).then(res => {
					this.list = res.data.list;
					console.log("list brand", this.list)
				})
			},
			inChange(val) {
				this.inited = true;
				console.log('vals', val)
				this.value = val;
				this.getList(val)
			}
		}
	}
</script>

<style>
</style>